package com.hxht.cmp.controller;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.CourtRoom;
import com.hxht.cmp.service.CaseReviewService;
import com.hxht.cmp.utils.ExportInternalUtil;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gaozikun on 2017/7/20 0020.
 */
@RestController
@RequestMapping("caseCount")
public class CaseCountController {

    @Resource
    private CaseReviewService caseReviewService;

    /**
     * 开庭次数统计
     *
     * @param courtUid
     * @param caseType
     * @param startDate
     * @param endDate
     * @param status    0表示单个法院的统计
     * @return
     */
    @RequestMapping("getCaseCount")
    public ResultResponse getCaseCount(String courtUid, String caseType, String startDate, String endDate, String status) {
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(status)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(caseReviewService.getCaseCount(courtUid, caseType, startDate, endDate, status));
    }

    /**
     * 开庭时长统计
     *
     * @param courtUid
     * @param caseType
     * @param startDate
     * @param endDate
     * @param status
     * @return
     */
    @RequestMapping("getCaseTimeCount")
    public ResultResponse getCaseTimeCount(String courtUid, String caseType, String startDate, String endDate, String status) {
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(status)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(caseReviewService.getCaseTimeCount(courtUid, caseType, startDate, endDate, status));
    }

    /**
     * 开庭准点率统计
     *
     * @param courtUid
     * @param startDate
     * @param endDate
     * @param status
     * @return
     */
    @RequestMapping("getOnTimeCount")
    public ResultResponse getOnTimeCount(String courtUid, String startDate, String endDate, String status) {
        if (ObjectUtils.isEmpty(courtUid) || ObjectUtils.isEmpty(status)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(caseReviewService.getOnTimeCount(courtUid, startDate, endDate, status));
    }

    /**
     * 获取案件类型，service写死
     *
     * @return
     */
    @RequestMapping("getAllCaseType")
    public ResultResponse getAllCaseType() {
        return ResultResponse.success(caseReviewService.getAllCaseType());
    }

    /**
     * 获取准点类型，service写死
     *
     * @return
     */
    @RequestMapping("getAllOnTimeType")
    public ResultResponse getAllOnTimeType() {
        return ResultResponse.success(caseReviewService.getAllOnTimeType());
    }

    /**
     * 获取法院案件对应的所有法官和书记员
     *
     * @param courtUid
     * @return
     */
    @RequestMapping("getJudgeAndSecretary")
    public ResultResponse getJudgeAndSecretary(String courtUid) {
        if (ObjectUtils.isEmpty(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(caseReviewService.selectJudgeAndSecretary(courtUid));
    }

    @RequestMapping("getTechnologyCourtRate")
    public ResultResponse technologyCourtRate(String courtUid) {
        if (ObjectUtils.isEmpty(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        return ResultResponse.success(caseReviewService.getTechnologyCourtRate(courtUid));
    }


    @RequestMapping("caseCountExport")
    public String exportCourtEquipment(HttpServletResponse response, String startDate, String endDate) {
        List<Map<String, Object>> result = caseReviewService.getCaseCountExport(startDate, endDate);
        try {
            String fileName = new String(("开庭次数统计" + startDate + "至" + endDate).getBytes("gb2312"), "iso8859-1") + ".xlsx";
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setCharacterEncoding("utf-8");
            // response.setHeader("Content-disposition", "attachment; filename="
            // + "exdddcel" + ".xlsx");// 组装附件名称和格式
            String[] titles = {"法院", "刑事", "民事", "行政", "赔偿", "执行", "信访", "其他", "总数", "备注"};
            ServletOutputStream outputStream = response.getOutputStream();
            ExportExcel(titles, result, outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void ExportExcel(String[] titles, List<Map<String, Object>> list, ServletOutputStream outputStream) {
        // 创建一个workbook 对应一个excel应用文件
        XSSFWorkbook workBook = new XSSFWorkbook();
        // 在workbook中添加一个sheet,对应Excel文件中的sheet
        //Sheet名称，可以自定义中文名称
        XSSFSheet sheet = workBook.createSheet("Sheet1");
        sheet.setColumnWidth(0, 40 * 256);
        sheet.setColumnWidth(1, 17 * 256);
        sheet.setColumnWidth(2, 17 * 256);
        sheet.setColumnWidth(3, 17 * 256);
        sheet.setColumnWidth(4, 17 * 256);
        sheet.setColumnWidth(5, 17 * 256);
        sheet.setColumnWidth(6, 17 * 256);
        sheet.setColumnWidth(7, 17 * 256);
        sheet.setColumnWidth(8, 17 * 256);
        sheet.setColumnWidth(9, 56 * 256);
        ExportInternalUtil exportUtil = new ExportInternalUtil(workBook, sheet);
        XSSFCellStyle headStyle = exportUtil.getHeadStyle();
        XSSFCellStyle bodyStyle = exportUtil.getBodyStyle();
        // 构建表头
        XSSFRow headRow = sheet.createRow(0);
        XSSFCell cell;

        // 输出标题
        for (int i = 0; i < titles.length; i++) {
            cell = headRow.createCell(i);
            cell.setCellStyle(headStyle);
            cell.setCellValue(titles[i]);
        }
        // 构建表体数据
        for (int j = 0; j < list.size(); j++) {
            XSSFRow bodyRow = sheet.createRow(j + 1);
            Map<String, Object> map = list.get(j);

            cell = bodyRow.createCell(0);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("courtName").toString());

            cell = bodyRow.createCell(1);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a1").toString());

            cell = bodyRow.createCell(2);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a2").toString());

            cell = bodyRow.createCell(3);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a5").toString());

            cell = bodyRow.createCell(4);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a4").toString());

            cell = bodyRow.createCell(5);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a3").toString());

            cell = bodyRow.createCell(6);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a6").toString());

            cell = bodyRow.createCell(7);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("a7").toString());

            cell = bodyRow.createCell(8);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue(map.get("count").toString());

            cell = bodyRow.createCell(9);
            cell.setCellStyle(bodyStyle);
            cell.setCellValue("");

        }

        try {
            workBook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
